package genia;

import genia.model.User;
import genia.util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Enumeration;

/*
* 登录功能
* genia
* */

public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //response.getWriter().write("Hello Genia, login please<br>");
        String name = request.getParameter("name");
        String pw = request.getParameter("password");

        HttpSession session = request.getSession();
        session.setAttribute("sessionID","sessionValue");

        //构造user对象，并赋值
        User user=new User();
        user.setUsername(name);
        user.setPassword(pw);

        //查询用户是否存在
        boolean loginSuccess = false;
        java.sql.Connection conn = null;
        java.sql.Statement stmt = null;
        ResultSet rs=null;
        //Driver driver;

        try {
            //util：用jdbc连接sql
              conn =  JdbcUtil.getConn();

            // 执行查询
            stmt = conn.createStatement();
            String sql;
            sql = " SELECT * FROM user where username = '"+user.getUsername()+"' and password = '"+user.getPassword()+"'";
            rs = stmt.executeQuery(sql);

            if(rs.next()){
//                用户名密码正确，重定向到main.jsp
                String username2 = rs.getString("username");
                String password2  = rs.getString("password");
                if(user.getUsername().equals(username2) && user.getPassword().equals(password2)){

                    request.getRequestDispatcher("/main.jsp").forward(request, response);
                }
            }
            else  {
//                request.setAttribute("errInfo", "用户名或密码不正确！");
//                response.setCharacterEncoding("utf-8");
                response.setContentType("text/html;setchar=utf-8");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }
        }catch(Exception se){
            // 处理 JDBC 错误
            se.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
                if (rs != null) rs.close();
            }
            catch (Exception se) {
                se.printStackTrace();
            }
        }
    }



}
